package com.campus.web.controller;


import com.campus.app.service.WxIndexImagesService;
import com.campus.commons.entity.WxIndexImage;
import com.campus.commons.entity.WxIndexImageType;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.oss.AliOssCloudUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 * 轮播图表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/wx-image")
public class WxIndexImageController {

    @Autowired
    private WxIndexImagesService wxIndexImagesService;

    @GetMapping("/")
    public ModelAndView toIndexImage(ModelMap modelMap) {
        // 需要 信息： 类型
        modelMap.addAttribute("types", WxIndexImageType.IndexTypes.values());
        return new ModelAndView("system/index_image/index",modelMap);
    }



    @ApiOperation(value = "查询全部轮播信息")
    @PostMapping("/all")
    public Result getAllIndexImages(MyPage<WxIndexImage> myPage) {
        return wxIndexImagesService.getAllIndexImages(myPage);
    }
    @ApiOperation(value = "根据页面查询轮播信息;必要参数 indexTypeId：页面类型, start=1;")
    @PostMapping("/search")
    public Result getIndexImages(MyPage<WxIndexImage> myPage, @Valid WxIndexImage wxIndexImage) {
        myPage.setData(wxIndexImage);
        return wxIndexImagesService.getIndexImages(myPage);
    }
    @ApiOperation("删除轮播信息")
    @PostMapping("/del")
    public Result delIndexImage(Integer id) {
        return wxIndexImagesService.delIndexImage(id);
    }

    /**
     * 添加
     * @param wxIndexImage
     * @param file
     * @return
     */
    public Result addIndexImage(WxIndexImage wxIndexImage, MultipartFile file) {
        return wxIndexImagesService.addIndexImage(wxIndexImage,file);
    }

    @PostMapping("/saveOrUpdate")
    public Result dispose(boolean isAdd,WxIndexImage wxIndexImage,MultipartFile file) {
        if (isAdd) {
            return addIndexImage(wxIndexImage, file);
        } else {
            return upIndexImage(wxIndexImage, file);
        }
    }

    /**
     * 更新轮播信息；
     * @param wxIndexImage
     * @param file
     * @return
     */
   public Result upIndexImage(WxIndexImage wxIndexImage,MultipartFile file) {
        return wxIndexImagesService.upIndexImage(wxIndexImage,file);
    }

    /**
     * 获取某个页面使用的轮播数量
     * @param indexTypeId
     * @return
     */
    @PostMapping("/startCount")
    public Integer getStartCount(Integer indexTypeId) {
        return wxIndexImagesService.getStartCount(indexTypeId, null);
    }


}

